/*******************************************************************************
 *                                    ZLG
 *                         ----------------------------
 *                         innovating embedded platform
 *
 * Copyright (c) 2001-present Guangzhou ZHIYUAN Electronics Co., Ltd.
 * All rights reserved.
 *
 * Contact information:
 * web site:    https://www.zlg.cn
 *******************************************************************************/
/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "core/include/hc32f4a0_pwc.h"
#include "common/hc32f4a0_errno.h"

/*******************************************************************************
 * Code
 ******************************************************************************/
/**
 * \brief 设置 PWC FCG0 外设时钟
 *
 * \param[in] fcg0_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg0_periph_clk_set(uint32_t fcg0_periph, pwc_state_t state){
    if (!((fcg0_periph != 0x00) &&
            ((fcg0_periph | PWC_FCG1_MASK) == PWC_FCG1_MASK))) {
        return -EPERM;
    }

    if (((state != PWC_DISABLE) && (state != PWC_ENABLE)) || (!IS_PWC_FCG0_UNLOCKED())) {
        return -EPERM;
    }

    if (state == PWC_ENABLE) {
        HC32F4A0_PWC->FCG0 &= ~(fcg0_periph);
    } else {
        HC32F4A0_PWC->FCG0 |= fcg0_periph;
    }

    return 0;
}

/**
 * \brief 设置 PWC FCG1 外设时钟
 *
 * \param[in] fcg1_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg1_periph_clk_set(uint32_t fcg1_periph, pwc_state_t state){

    if (!((fcg1_periph != 0x00) &&
            ((fcg1_periph | PWC_FCG1_MASK) == PWC_FCG1_MASK))) {
        return -EPERM;
    }

    if ((state != PWC_DISABLE) && (state != PWC_ENABLE)) {
        return -EPERM;
    }

    if (state == PWC_ENABLE) {
        HC32F4A0_PWC->FCG1 &= ~(fcg1_periph);
    } else {
        HC32F4A0_PWC->FCG1 |= fcg1_periph;
    }

    return 0;
}

/**
 * \brief 设置 PWC FCG2 外设时钟
 *
 * \param[in] fcg2_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg2_periph_clk_set(uint32_t fcg2_periph, pwc_state_t state){
    if (!((fcg2_periph != 0x00) &&
            ((fcg2_periph | PWC_FCG3_MASK) == PWC_FCG3_MASK))) {
        return -EPERM;
    }

    if ((state != PWC_DISABLE) && (state != PWC_ENABLE)) {
        return -EPERM;
    }

    if (state == PWC_ENABLE) {
        HC32F4A0_PWC->FCG2 &= ~(fcg2_periph);
    } else {
        HC32F4A0_PWC->FCG2 |= fcg2_periph;
    }
    return 0;
}

/**
 * \brief 设置 PWC FCG3 外设时钟
 *
 * \param[in] fcg3_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg3_periph_clk_set(uint32_t fcg3_periph, pwc_state_t state){
    if (!((fcg3_periph != 0x00) &&
            ((fcg3_periph | PWC_FCG3_MASK) == PWC_FCG3_MASK))) {
        return -EPERM;
    }

    if ((state != PWC_DISABLE) && (state != PWC_ENABLE)) {
        return -EPERM;
    }

    if (state == PWC_ENABLE) {
        HC32F4A0_PWC->FCG3 &= ~(fcg3_periph);
    } else {
        HC32F4A0_PWC->FCG3 |= fcg3_periph;
    }

    return 0;
}



